package net.zjitc.controller.client;

import net.zjitc.common.ResultMessage;
import net.zjitc.pojo.*;
import net.zjitc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/24 11:21
 */
@Controller
@RequestMapping(value = "/Cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductService productService;
    @Autowired
    OrderService orderService;
    @Autowired
    OrderItemService orderItemService;

    public void top(Model model){
        List<Category> list = categoryService.findAll();
        model.addAttribute("list",list);
    }

    public void user_news(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<Cart> cartList = cartService.findByUid(user.getId());
        for (Cart c:cartList){
            Product product = productService.findById(c.getPid());
            c.setProduct(product);
        }
        model.addAttribute("cart",cartList);
    }

    @RequestMapping(value = "/mycart")
    public String mycart(HttpServletRequest request,Model model){
        top(model);
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        List<Cart> cartList = cartService.findByUid(user.getId());
        for (Cart c:cartList){
            Product product = productService.findById(c.getPid());
            c.setProduct(product);
        }
        model.addAttribute("cart",cartList);
        return "/client/cart.jsp";
    }
    @RequestMapping(value = "/myorder")
    public String myorder(HttpServletRequest request, Model model){
        top(model);
        user_news(request,model);
        return "/client/order.jsp";
    }
    @RequestMapping(value = "/addCart")
    @ResponseBody
    public ResultMessage addNotice(Cart cart,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        cart.setUid(user.getId());
        int add= cartService.add(cart);
        if (add>0){
            return ResultMessage.createSuccessMessage("添加成功",0);

        }
        return ResultMessage.createErrorMessage("添加失败",1);
    }


    @RequestMapping(value = "/updateNumber")
    @ResponseBody
    public ResultMessage updateNumber(@RequestBody Cart cart,
                                      HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        cart.setUid(user.getId());
        int resultCount = cartService.updateNumber(cart);
        if (resultCount>0){
            return ResultMessage.createSuccessMessage("修改成功",0);
        }
        return ResultMessage.createErrorMessage("修改失败",1);
    }


    @RequestMapping("/delete")
    @ResponseBody
    public ResultMessage delete(Integer id) {
        int resultCount = cartService.delete(id);
        if (resultCount > 0) {
            return ResultMessage.createSuccessMessage("商品移除成功",0);
        }
        return ResultMessage.createErrorMessage("商品移除失败",1);
    }


    @RequestMapping(value = "/createOrder")
    public String createOrder(Model model, HttpServletRequest request, Orders orders){
        top(model);
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        orders.setUser_id(user.getId());
        orders.setReceiverName(request.getParameter("receiverName"));
        orders.setReceiverAddress(request.getParameter("receiverAddress"));
        orders.setReceiverPhone(request.getParameter("receiverPhone"));
        orders.setMoney(Double.valueOf(request.getParameter("money")));
         int result=orderService.add(orders);
         if (result>0){
             Orderitem orderitem = new Orderitem();
             orderitem.setOid(orders.getId());
             List<Cart> cartList = cartService.findByUid(orders.getUser_id());
             for (Cart c:cartList){
                 orderitem.setPid(c.getPid());
                 orderitem.setBuynum(c.getNumber());
                orderItemService.add(orderitem);
                cartService.delete(c.getId());

             }
             return "/finishOrder.jsp";
         }
        return "/client/orderInfo.jsp";
    }



}
